package xin.marcher.wind.migrate.order.mock;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.RandomUtils;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Arrays;

/**
 * 插入数据工具类
 */
public class InsertDataService {

    private final long offset = 100;

    // 起始 id
    private long begin = 0;
    // 每次循环插入的数据量, 默认 100
    private long end = begin + offset;
    private String url = "jdbc:mysql://localhost:3306/marcher_migrate_demo_order?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT";
    private String user = "root";
    private String password = "123456";
    // 订单状态
    private Integer[] orderStatus = {10, 20, 30, 40, 50, 55, 60, 70, 100};

    public void insertBigData() {
        // 定义连接、statement 对象
        Connection conn;
        PreparedStatement orderInfoPstm;
        PreparedStatement orderItemPstm;
        try {
            // 编写 sql
            String orderInfoSql = "insert into order_info(order_id, user_id, order_type, order_status, total_amount, pay_amount, creator, create_time, updater, update_time) values (?,?,?,?,?,?,?,?,?,?);";
            String orderItemSql = "insert into order_item(order_id, product_type, product_id, sale_quantity, sale_price, creator, create_time, updater, update_time) values (?,?,?,?,?,?,?,?,?);";

            // 加载 jdbc 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 连接 mysql
            conn = DriverManager.getConnection(url, user, password);
            // 将自动提交关闭
            conn.setAutoCommit(false);

            // 预编译 sql
            orderInfoPstm = conn.prepareStatement(orderInfoSql);
            orderItemPstm = conn.prepareStatement(orderItemSql);

            // 开始总计时
            long bTime1 = System.currentTimeMillis();

            // 两层循环生成一万条数据
            for (int i = 0; i < 100 ; i++) {
                // 开启分段计时
                long bTime = System.currentTimeMillis();
                long userId = GenOrderNoUtil.createNo(100000);

                // 开始循环
                while (begin < end) {
                    Date createDate = GenOrderNoUtil.getCreateOrderNoDate();
                    String orderId = GenOrderNoUtil.getOrderNo(userId);
                    Long productId = GenOrderNoUtil.createNo(100000);
                    Timestamp updateTime = DateUtil.offsetSecond(createDate, RandomUtils.nextInt(20, 30)).toTimestamp();
                    int orderType = 1;

                    // 赋值
                    orderInfoPstm.setString(1, orderId);
                    orderInfoPstm.setLong(2, userId);
                    orderInfoPstm.setInt(3, orderType);
                    orderInfoPstm.setLong(4, GenOrderNoUtil.getStatus(Arrays.asList(orderStatus)));
                    orderInfoPstm.setBigDecimal(5, BigDecimal.ZERO);
                    orderInfoPstm.setBigDecimal(6, BigDecimal.ZERO);
                    orderInfoPstm.setString(7, String.valueOf(userId));
                    orderInfoPstm.setTimestamp(8, new Timestamp(createDate.getTime()));
                    orderInfoPstm.setString(9, String.valueOf(userId));
                    orderInfoPstm.setTimestamp(10, updateTime);
                    orderInfoPstm.execute();

                    orderItemPstm.setString(1, orderId);
                    orderItemPstm.setInt(2, 1);
                    orderItemPstm.setLong(3, productId);
                    orderItemPstm.setInt(4, 10);
                    orderItemPstm.setInt(5, 100);
                    orderItemPstm.setString(6, String.valueOf(userId));
                    orderItemPstm.setTimestamp(7, new Timestamp(createDate.getTime()));
                    orderItemPstm.setString(8, String.valueOf(userId));
                    orderItemPstm.setTimestamp(9, updateTime);
                    orderItemPstm.execute();
                    begin++;
                }

                // 执行批处理
//                orderInfoPstm.addBatch();
//                orderInfoPstm.executeBatch();
                // 执行批处理
//                orderItemPstm.addBatch();
//                orderItemPstm.executeBatch();

                // 提交事务
                conn.commit();
                // 边界值
                end += offset;
                // 关闭分段计时
                long eTime = System.currentTimeMillis();

                System.out.println("插入数据耗时：" + (eTime - bTime));
            }

            long eTime1 = System.currentTimeMillis();
            //输出
            System.out.println("插入数据共耗时：" + (eTime1 - bTime1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
